package util

import (
	"fmt"
	"sort"
)

type stuScore struct {
	name  string
	score float64
}

type stus []stuScore

func (s stus) Len() int {
	return len(s)
}

func (s stus) Less(i, j int) bool {
	return s[i].score < s[j].score
}

func (s stus) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func TestSort() {
	s1 := []int{1, 5, 3, 9, 11, 2}
	sort.Ints(s1)
	fmt.Println(s1)

	s2 := []string{"sfsd", "edfer", "dfe", "rsfe"}
	sort.Strings(s2)
	fmt.Println(s2)

	s11 := []int{1, 5, 3, 9, 11, 2}
	sort.Slice(s11, func(i, j int) bool {
		return s11[i] < s11[j]
	})
	fmt.Println(s11)

	s22 := []string{"sfsd", "edfer", "dfe", "rsfe"}
	sort.Slice(s22, func(i, j int) bool {
		return s22[i] < s22[j]
	})
	fmt.Println(s22)

	stus1 := []stuScore{
		{"张三门", 95},
		{"李四", 91},
		{"赵五", 9},
		{"王六", 90},
	}
	//sort排序  必须实现Interface接口
	sort.Sort(stus(stus1))
	fmt.Println(stus1)

	stus2 := []stuScore{
		{"张三门", 95},
		{"李四", 91},
		{"赵五", 9},
		{"王六", 90},
	}

	//Slice排序，传入slice，和compare函数
	sort.Slice(stus2, func(i, j int) bool {
		return stus2[i].score < stus2[j].score
	})
	fmt.Println(stus2)
}
